package com.ayu.scalalearn.chapter08

/**
  * @Author 徐林玉
  * @Description Test03MatchTupleExtend
  * @Date 2022/8/18 10:09
  * @Version 1.0
  */
object Test03MatchTupleExtend {

  def main(args: Array[String]): Unit = {
    //1、在变量声明时匹配
    var (x, y) = (10, "hello")

    println(s"x: $x,y:$y")

    val List(first, second, _*) = List(11, 12, 9, 10)

    println(s"first:$first,second:$second")


    val fir :: sec :: rest = List(1, 2, 3, 4, 5)

    println(s"first:$first,second:$second,rest:$rest")

    println("=================================")

    // 2. for推导式中进行模式匹配
    val list: List[(String, Int)] = List(("a", 12), ("b", 35), ("c", 27), ("a", 13))

    //2.1原本的遍历方式
    for (elem <- list) {

      println(elem._1 + "  " + elem._2)
    }


    //2.2将list元素直接定义为元组，对变量赋值
    for ((word, count) <- list) {

      println(word + " " + count)
    }

    //2.3可以不考虑某个位置的变量，只遍历key和value
    for ((word, _) <- list) {
      println(word)
//      println(word + "====" + _)
    }

    println("--------------------------")

    // 2.4 可以指定某个位置的值必须是多少
    for (("a", count) <- list){
      println(count)
    }

  }

}
